<template>
  <div class="top-navbar">
    <div class="navbar-content">
      <!-- 左侧Logo区域 -->
      <div class="navbar-left">
        <div class="logo-container">
          <div class="logo-icon">
            <img src="/images/logo.png" alt="logo" class="logo-img" />
          </div>
          <div class="logo-text">
            <div class="company-name">智碳未来</div>
            <div class="system-name">能源管理系统</div>
          </div>
        </div>
      </div>

      <!-- 中间导航菜单 -->
      <div class="navbar-center">
        <div class="nav-menu">
          <div
            v-for="module in moduleList"
            :key="module.path"
            :class="['nav-item', { active: selectedModule === module.path }]"
            @click="handleModuleChange(module.path)"
          >
            <svg-icon :icon-class="module.meta.icon" class="nav-icon" />
            <span class="nav-text">{{ module.meta.title }}</span>
          </div>
        </div>
      </div>

      <!-- 右侧用户区域 -->
      <div class="navbar-right">
        <div class="user-info">
          <el-badge :value="12" class="notification-badge">
            <el-icon class="notification-icon">
              <Bell />
            </el-icon>
          </el-badge>
          <div class="user-avatar">
            <el-avatar
              :size="32"
              src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png"
            />
            <span class="username">zhangyu-test</span>
          </div>
          <el-icon class="help-icon">
            <QuestionFilled />
          </el-icon>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, computed } from "vue";
import { Bell, QuestionFilled } from "@element-plus/icons-vue";
import useAppStore from "@/store/modules/app";
import usePermissionStore from "@/store/modules/permission";
import { useRouter } from "vue-router";

const appStore = useAppStore();
const permissionStore = usePermissionStore();
const router = useRouter();

// 从权限store获取动态菜单
const moduleList = computed(() => {
  const routes = permissionStore.sidebarRouters || [];
  // 只显示顶级模块（有children属性的），并且不是外部链接
  const filtered = routes.filter(route => {
    return !route.hidden && 
           route.meta && 
           route.meta.title && 
           route.children && 
           route.children.length > 0 &&
           !route.meta.link; // 过滤掉外部链接
  });
  return filtered;
});

const selectedModule = ref("");

// 处理模块切换
const handleModuleChange = (value) => {
  selectedModule.value = value;
  const module = moduleList.value.find((m) => m.path === value);
  if (module) {
    appStore.setCurrentModule(module);
    
    // 检查是否为外部链接
    if (module.path && (module.path.startsWith('http://') || module.path.startsWith('https://'))) {
      // 外部链接，直接打开新窗口
      window.open(module.path, '_blank');
      return;
    }
    
    // 直接使用模块的children作为子菜单
    if (module.children && module.children.length > 0) {
      appStore.updateSidebarMenu(module.children);
      
      // 构建第一个子菜单的完整路径
      const firstChild = module.children[0];
      let targetPath = firstChild.path;
      
      // 如果子菜单路径不是以 / 开头，需要拼接父路径
      if (!targetPath.startsWith('/')) {
        targetPath = module.path + '/' + targetPath;
      }
      
      router.push(targetPath);
    } else {
      // 如果没有子菜单，清空侧边栏菜单
      appStore.updateSidebarMenu([]);
      // 直接跳转到当前路径
      router.push(module.path);
    }
  }
};

onMounted(() => {
  // 等待路由加载完成后再设置默认模块
  if (moduleList.value.length > 0) {
    // 查找首页模块（title为"首页"的模块）
    const homeModule = moduleList.value.find(module => 
      module.meta && module.meta.title === "首页"
    );
    
    if (homeModule) {
      selectedModule.value = homeModule.path;
      handleModuleChange(homeModule.path);
    } else {
      // 如果找不到首页模块，使用第一个模块
      const firstModule = moduleList.value[0];
      selectedModule.value = firstModule.path;
      handleModuleChange(firstModule.path);
    }
  }
});
</script>

<style lang="scss" scoped>
.top-navbar {
  height: 60px;
  background: linear-gradient(135deg, #20a0ff 0%, #4fc3f7 100%);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  position: relative;
  z-index: 1000;
}

.navbar-content {
  height: 100%;
  display: flex;
  align-items: center;
  padding: 0 20px;
  width: 100%;
}

.navbar-left {
  flex: 0 0 auto;
  margin-right: 40px;
}

.logo-container {
  display: flex;
  align-items: center;
  gap: 12px;
}

.logo-icon {
  width: 40px;
  height: 40px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;

}

.logo-img {
  width: 32px;
  height: 32px;
  object-fit: contain;
}

.logo-text {
  color: #fff;
}

.company-name {
  font-size: 18px;
  font-weight: 600;
  line-height: 1.2;
}

.system-name {
  font-size: 12px;
  opacity: 0.8;
  line-height: 1.2;
}

.navbar-center {
  flex: 1;
  display: flex;
  justify-content: center;
}

.nav-menu {
  display: flex;
  gap: 8px;
  align-items: center;
}

.nav-item {
  display: flex;
  align-items: center;
  padding: 8px 16px;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.3s ease;
  color: rgba(255, 255, 255, 0.9);
  font-size: 14px;
  font-weight: 500;
  white-space: nowrap;

  &:hover {
    background: rgba(255, 255, 255, 0.15);
    color: #fff;
    transform: translateY(-1px);
  }

  &.active {
    background: rgba(255, 255, 255, 0.25);
    color: #fff;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
  }

  .nav-icon {
    margin-right: 6px;
    font-size: 20px;
    color: #fff;
  }

  .nav-text {
    font-size: 14px;
  }
}

.navbar-right {
  flex: 0 0 auto;
  margin-left: 40px;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 16px;
}

.notification-badge {
  cursor: pointer;
}

.notification-icon {
  font-size: 20px;
  color: rgba(255, 255, 255, 0.9);
  transition: color 0.3s;

  &:hover {
    color: #fff;
  }
}

.user-avatar {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 6px;
  transition: background 0.3s;

  &:hover {
    background: rgba(255, 255, 255, 0.1);
  }
}

.username {
  color: #fff;
  font-size: 14px;
  font-weight: 500;
}

.help-icon {
  font-size: 18px;
  color: rgba(255, 255, 255, 0.9);
  cursor: pointer;
  transition: color 0.3s;

  &:hover {
    color: #fff;
  }
}
</style>
